//
//  ChatBarFaceViewButtomView.m
//  keyboard
//
//  Created by 遇水 on 16/3/12.
//  Copyright © 2016年 遇水. All rights reserved.
//

#import "ChatBarFaceViewButtomView.h"
#import "UIView+Extension.h"



@interface ChatBarFaceViewButtomView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView    *scrollView;

@property (nonatomic, strong) UIButton    *selectedButton;

@property (nonatomic, strong) UIButton    *sendButton;

@end

@implementation ChatBarFaceViewButtomView



-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubview];
    }
    
    return  self;
}


-(void)initSubview
{
    //判断设备为
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.pagingEnabled = YES;
    scrollView.delegate  = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:scrollView];
    self.scrollView = scrollView;

    
    //添加一个button
    [self addSubview:self.sendButton = [self setupButton:@"发送" image:@"send-background" selectedImage:nil buttonType:FaceViewButtomViewButtonTypeSend]];
    
    //给scrollView添加子控件, 此处设置默认按钮是无用的，因为没有设置代理，所以不会调用代理方法，无法成功
    UIButton *button =[self setupButton:@"默认" image:nil selectedImage:@"face-selected" buttonType:FaceViewButtomViewButtonTypeDefault];
    [self.scrollView addSubview:button];
    
    
      [self.scrollView addSubview:[self setupButton:@"emoji" image:nil selectedImage:@"face-selected" buttonType:FaceViewButtomViewButtonTypeEmoji]];
      [self.scrollView addSubview:[self setupButton:@"浪小花" image:nil selectedImage:@"face-selected" buttonType:FaceViewButtomViewButtonTypeLXH]];
      [self.scrollView addSubview:[self setupButton:@"最近" image:nil selectedImage:@"face-selected" buttonType:FaceViewButtomViewButtonTypeNewly]];
      [self.scrollView addSubview:[self setupButton:@"自定义" image:nil selectedImage:@"face-selected" buttonType:FaceViewButtomViewButtonTypeCustom]];
}

#pragma mark private
-(UIButton *)setupButton:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage  buttonType:(FaceViewButtomViewButtonType)type
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (type) {
         button.tag = type;
    }
    
    [button setTitle:title forState:UIControlStateNormal];
    if (type != FaceViewButtomViewButtonTypeSend) {
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    }else{
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }

    if (image) {
        [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    
    if (selectedImage) {
        [button setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled];
    }

    return button;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat buttonW =[[UIScreen mainScreen] bounds].size.width / 4;
    //判断当前屏幕是否大于iPhone 5 ,就按照568 的宽度1/4 来，
   if ([[UIScreen mainScreen] bounds].size.height > 568 ){
       buttonW = 320 / 4;
    }
            self.scrollView.x = 0;
            self.scrollView.y = 0;
            self.scrollView.width = buttonW * 3;
            self.scrollView.height = self.height;

            self.sendButton.x = buttonW * 3;
            self.sendButton.y = 0;
            self.sendButton.width = buttonW;
            self.sendButton.height = self.height;

    NSInteger count = self.scrollView.subviews.count;
    
    //设置滚动视图中的子控件
    for (NSInteger i = 0; i < count; i++) {
        UIButton *button = (UIButton *)self.scrollView.subviews[i];
        button.width = buttonW;
        button.x = i * button.width;
        button.y = 0;
        button.height = self.scrollView.height;
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.subviews.count * buttonW, 0);

}



-(void)setDelegate:(id<ChatBarFaceViewButtomViewDelegate>)delegate
{
    _delegate = delegate;
    //设置默认选中的按钮
    for (UIButton *button in self.scrollView.subviews) {
        if (button.tag == FaceViewButtomViewButtonTypeDefault) {
            [self buttonAction:button];
        }
    }
    
}

-(void)buttonAction:(UIButton *)button
{
    if (button.tag == FaceViewButtomViewButtonTypeSend) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationSendMessage object:nil];
    }else{
        self.selectedButton.enabled = YES;
        button.enabled = NO;
        self.selectedButton = button;
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarFaceViewButtomView:didSelectButton:)]) {
            [self.delegate chatBarFaceViewButtomView:self didSelectButton:button.tag];
        }
    }
}



-(UIImage *)createImageWithColor:(UIColor *)color frame:(CGRect *)frame
{
    UIGraphicsBeginImageContext(frame->size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, *frame);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}




@end
